var express = require('express');
var router = express.Router();
var db = require("./sqlite3");

/* GET home page. */
router.get('/', function(req, res, next) {
    var name = req.query.name;
    db.querySomeMemberInfo(name, function(err, rows) {
        db.queryAllMemberInfo(function(err, rows) {
            res.render('index', {
                title: 'EasyFood',
                member: rows,
                consumeinfo:null
            });
        }, function() {
            res.render('info', {
                title: "info",
                info: "query failed!"
            });
        }, router);
    }, function() {
        res.render('info', {
            title: "info",
            info: "You have no access to this Infomation!"
        });
    }, router);
});

router.get('/consume', function(req, res, next) {
    var name = req.query.name;
    db.querySomeMemberInfo(name, function(err, rows) {
        db.queryAllMemberInfo(function(err, rows) {
            res.render('consume', {
                title: 'EasyFood',
                member: rows
            });
        }, function() {
            res.render('info', {
                title: "info",
                info: "query failed!"
            });
        }, router);
    }, function() {
        res.render('info', {
            title: "info",
            info: "You have no access to this Infomation!"
        });
    }, router);
});

router.post('/aashare',function(req, res, next) {
    debugger;
    var body=req.body;
    var manager = body.verification.manager;
    var word = body.verification.word;
    db.checkIsManager(manager,
        word,
        function(err, rows) {
            console.log(rows);
            var costinfo = body.costinfo;
            db.aaShare(costinfo, function(consumeInfo) {
                console.log("message");
                db.queryAllMemberInfo(function(err, rows) {
                    console.log("render");
                    res.render('index', {
                        title: 'EasyFood',
                        member: rows,
                        consumeinfo:consumeInfo
                    });
                }, function() {
                    res.render('info', {
                        title: "info",
                        info: "query failed!"
                    });
                }, router);
            }, router);
        },
        function(err, rows) {
            res.render('info', {
                title: "info",
                info: "You have no access to this Infomation!"
            });
        },
        router);
});

router.get('/addmember', function(req, res, next) {
    var name = req.query.name,
        money = req.query.money,
        manager = req.query.manager,
        word = req.query.word;
    db.checkIsManager(manager, word, function() {
        db.addMember(name, money, function() {
            db.queryTableInfo("member", "", function(err, rows) {
                res.render('index', {
                    title: 'EasyFood addmember:' + name + " succese!",
                    member: rows
                });
            }, router);
        }, router);
    }, function() {
        res.render('info', {
            title: "info",
            info: "You have no access to this Infomation!"
        });
    }, router);
});

router.post('/testpost', function(req, res, next) {
    console.log("params:" + req.params.name);
    console.log("query:" + req.query.name);
});

module.exports = router;